package com.shw.pclient.ui;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.TextView;

import com.shw.pclient.R;
import com.shw.pclient.adapter.SHWGPSHandler;
import com.shw.pclient.adapter.SHWHomeAdapter;
import com.shw.pclient.adapter.SHWImageAdapter;
import com.shw.pclient.adapter.SHWNetManager;
import com.shw.pclient.adapter.SHWTrigger;
import com.shw.pclient.adapter.SHWUpLoadImage;
import com.shw.pclient.util.StringTools;
import com.shw.pclient.vo.HomeInfo;
import com.shw.pclient.vo.UtilInfo;

/**
 * 
 * @author HAHA
 * 
 */
@SuppressLint("NewApi")
public class HomeActivity extends Activity {

	private GridView gridView;
	private List<HomeInfo> homeInfos;
	private SHWHomeAdapter homeAdapter;
	private TextView navBackView;
	private TextView navTitleView;
	private TextView navHomeView;
	private String filenames;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.home);
		// getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,R.layout.home_header);
		navBackView = (TextView) findViewById(R.id.NavigateBack);
		navBackView.setOnClickListener(new NavBackViewClickListener());
		navTitleView = (TextView) findViewById(R.id.NavigateTitle);
		navTitleView.setText(R.string.shw_home);
		navHomeView = (TextView) findViewById(R.id.NavigateHome);
		navHomeView.setOnClickListener(new NavHomeViewClickListener());
		gridView = (GridView) findViewById(R.id.gridview);
		homeInfos = new ArrayList<HomeInfo>();
		homeInfos.add(new HomeInfo(R.string.shw_info, R.drawable.shwinfo, 1));
		homeInfos.add(new HomeInfo(R.string.shw_local, R.drawable.shwlocal, 2));
		homeInfos.add(new HomeInfo(R.string.shw_image, R.drawable.shwimage, 3));
		homeInfos
				.add(new HomeInfo(R.string.shw_carema, R.drawable.shwcarema, 4));
		homeInfos.add(new HomeInfo(R.string.shw_trace, R.drawable.shwtrace, 5));
		// homeInfos.add(new HomeInfo(R.string.shw_account,
		// R.drawable.shwaccount,6));
		homeAdapter = new SHWHomeAdapter(this);
		homeAdapter.setList(homeInfos);
		gridView.setAdapter(homeAdapter);
		gridView.setOnItemClickListener(new HomeGridViewClickListener());
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 1, R.string.app_exit);
		menu.add(0, 2, 2, R.string.app_about);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode != 0) {
			return;
		}
		super.onActivityResult(requestCode, resultCode, data);
		try {
			SHWImageAdapter.vector.add(filenames);
			String path = Environment.getExternalStorageDirectory().getPath()
					+ "/SHW/img/" + filenames;
			SHWTrigger.openConnect(this);
			File file = new File(path);
			if(file.exists())
			SHWUpLoadImage.executorService.execute(new SHWUpLoadImage(getBytes(file)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		startActivity(this.getIntent());
	}

	public byte[] getBytes(File file) {
		byte[] buffer = null;
		try {
			FileInputStream fis = new FileInputStream(file);
			ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);
			byte[] b = new byte[1024];
			int n;
			while ((n = fis.read(b)) != -1) {
				bos.write(b, 0, n);
			}
			fis.close();
			bos.close();
			buffer = bos.toByteArray();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return buffer;
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			CancelDialog();
		}
		return false;
	}

	public void CancelDialog() {
		Builder builder = new Builder(HomeActivity.this);
		builder.setMessage(R.string.home_dia_msage);
		builder.setTitle(R.string.home_dia_title);
		builder.setPositiveButton(R.string.home_dia_submit,
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
						HomeActivity.this.finish();
					}
				});
		builder.setNegativeButton(R.string.home_dia_cancle,
				new android.content.DialogInterface.OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		// android.os.Process.killProcess(android.os.Process.myPid());
		builder.create().show();
	}

	class NavBackViewClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {
			CancelDialog();
		}

	}

	class NavHomeViewClickListener implements OnClickListener {

		@Override
		public void onClick(View v) {

		}

	}

	class HomeGridViewClickListener implements OnItemClickListener {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			HomeInfo homeInfo = (HomeInfo) arg0.getItemAtPosition(arg2);
			switch (homeInfo.getSeq()) {
			case UtilInfo.SHW_INFO:
				Intent work_intent = new Intent();
				work_intent.setClass(HomeActivity.this, WorkingActivity.class);
				startActivity(work_intent);
				System.out.println(1);
				break;
			case UtilInfo.SHW_LOCAL:
				SHWTrigger.openConnect(HomeActivity.this);
				SHWTrigger.openGPS(HomeActivity.this);
				SHWGPSHandler.GPSInfo(HomeActivity.this);
				SHWNetManager netManager=new SHWNetManager(HomeActivity.this);
				netManager.startSendRevThread();
				System.out.println(2);
				break;
			case UtilInfo.SHW_IMAGE:
				Intent show_intent = new Intent();
				show_intent.setClass(HomeActivity.this, ImageActivity.class);
				startActivity(show_intent);
				System.out.println(3);
				break;
			case UtilInfo.SHW_CAREMA:
				Intent take_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
				String fileName = StringTools.getDateTimeSecond() + ".jpg";
				File dis = new File(Environment.getExternalStorageDirectory()
						.getPath() + "/SHW/img");
				if (!dis.exists()) {
					dis.mkdirs();
				}
				File out = new File(Environment.getExternalStorageDirectory()
						.getPath() + "/SHW/img", fileName);
				Uri uri = Uri.fromFile(out);
				take_intent.putExtra("imgName", fileName);
				take_intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
				filenames = fileName;
				startActivityForResult(take_intent, 0);
				break;
			case UtilInfo.SHW_TRACE:
				SHWGPSHandler.setSOSInfo();
				System.out.println(5);
				break;
			case UtilInfo.SHW_ACCOUNT:
				System.out.println(6);
				break;
			default:
				break;
			}
		}

	}

}
